define(['jquery', 'bootstrap', 'backend', 'form', 'table'], function ($, undefined, Backend, Form, Table) {
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/index',
                    add_url: '',
                    edit_url: '',
                    del_url: '',
                    multi_url: '',
                    table: 'user'
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                sortName: 'id',
                columns: [
                    [
                        {field: 'id', title: __('Id'), operate: false},
                        {field: 'number', title: __('Number'), operate: 'LIKE %...%'},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE %...%'},
                        {field: 'user_real_name.auth_table', title: __('Auth Table'), operate: false, events: Controller.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'user_real_name.handheld_card', title: __('Handheld Card'), operate: false, events: Controller.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'createtime', title: __('Createtime'), formatter: Table.api.formatter.datetime},
                    ]
                ],
                //禁用默认搜索
                search: false,
                //启用普通表单搜索
                commonSearch: true,
                //可以控制是否默认显示搜索单表,false则隐藏,默认为false
                showExport: false,
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

        },
        realname: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/realname',
                    add_url: '',
                    edit_url: '',
                    del_url: '',
                    multi_url: '',
                    table: 'user'
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                sortName: 'id',
                columns: [
                    [
                        {field: 'id', title: __('Id'), operate: false},
                        {field: 'number', title: __('Number'), operate: false},
                        {field: 'mobile', title: __('Mobile'), operate: false},
                        {field: 'auth_table', title: __('Auth Table'), operate: false, events: Controller.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'handheld_card', title: __('Handheld Card'), operate: false, events: Controller.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'createtime', title: __('Createtime'), formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Controller.api.formatter.confirm}
                    ]
                ],
                //禁用默认搜索
                search: false,
                //启用普通表单搜索
                commonSearch: true,
                //可以控制是否默认显示搜索单表,false则隐藏,默认为false
                searchFormVisible: true,
                showExport: false,
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {//渲染的方法
                confirm: function (value, row, index) {
                    return '<a class="btn btn-xs btn-success btn-change btn-confirm" data-url="/admin/user/confirm" data-id="' + row.id + '">' + __('Confirm') + '</a>' +
                            ' <a class="btn btn-xs btn-danger btn-change btn-noconfirm" data-url="/admin/user/unqualified" data-id="' + row.id + '">' + __('Unqualified') + '</a>';
                },
            },
            events: {//绑定事件的方法
                image: {
                    //格式为：方法名+空格+DOM元素
                    'click .img-center': function (e, value, row, index) {
                        e.stopPropagation();
                        var w = (window.screen.width - 30) + 'px', h = (window.screen.height - 70) + 'px';
                        if (window.screen.height > 600 && window.screen.width > 600) {
                            w = '1000px';
                            h = '600px';
                        }
                        layer.open({
                            type: 1,
                            title: false,
                            closeBtn: 1,
                            area: 'auto',
                            skin: 'layui-layer-nobg', //没有背景色
                            shadeClose: true,
                            content: '<img style="max-width:' + w + ';max-height:' + h + ';" src="' + $(e.currentTarget).attr('src') + '">',
                            success: function () {
                                $('.layui-layer').css('width', 'auto');
                            }
                        });
                    }
                },
            }
        },

    };
    return Controller;
});
